<?php
namespace app\admin\servlet;

class LoginServlet extends BaseServlet
{
	public function __construct()
	{
		parent::__construct();
	}

	/**
	* 组装查询条件
	* @param array $data
	* @return array
	*/
	public function getWhere($data = [])
	{
		$where = [];
		
		if( empty($data) )
		{
			return $where;
		}

		if ( empty($data['username']) === false )
        {
            $where[] = ['username', '=', $data['username']];
        }
		
		return $where;
	}

	/**
	* 整理数据
	* @param array $data
	* @return array
	*/
	public function collating($data = [])
	{
		if( empty($data) )
		{
			return $data;
		}

		return $data;
	}

    /**
     * 登录会话信息
     * @param $user
     */
    public function saveLogin($user)
    {
        //session
        session('uid', $user['uid']);
	}

    /**
     * 验证user信息
     * @param array $data
     */
    public function checkUser($user = [], $password = '')
    {
        //账号不存在
        if ( $user == false )
        {
            $this->error = '账号或密码错误';

            return false;
        }

        //验证密码
        if ( empty($password) === false && md5($password, $user['salt']) !== $user['password'] )
        {
            $this->error = '账号或密码错误';

            return false;
        }

        //状态是否正常
        if ( $user['status'] === 1 )
        {
            $this->error = '账号已被冻结';

            return false;
        }
	}
}